<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        width: 300px;
        display: flex;
        justify-content: space-between;
      }

      .red-green-light {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        width: 70px;
        height: 240px;
        background-color: #333;
        border-radius: 5px;
      }

      .light {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        background-color: #fff;
      }

      .red-green-light .light:nth-child(1).active {
        background-color: red;
      }

      .red-green-light .light:nth-child(2).active {
        background-color: green;
      }

      .red-green-light .light:nth-child(3).active {
        background-color: goldenrod;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <div class="red-green-light">
        <div class="light red"></div>
        <div class="light green"></div>
        <div class="light yellow"></div>
      </div>
    </div>
  </body>

  <script>
    // 让灯亮的方法
    const openLight = (node) => node.classList.add('active')
    // 让灯灭的方法
    const offLight = (node) => node.classList.remove('active')
    const light = document.querySelectorAll('.light')
    const lightPro=Array.from(light)
  
    /*1.async写法
    function clearTime(t){
      return new Promise(resolve=>{
        setTimeout(resolve,1000*t,)
      })
    }
    async function fn(){
      openLight(lightPro[0])
      await clearTime(3)
      offLight(document.querySelector('.active'))
      openLight(lightPro[1])
      await clearTime(2)
      offLight(document.querySelector('.active'))
      openLight(lightPro[2])
      await clearTime(1)
      offLight(document.querySelector('.active'))
      fn()
    }
    fn()*/


    /*2.同步写法
    function lit(){
      openLight(lightPro[0])
      setTimeout(() => {
      offLight(document.querySelector('.active'))
      openLight(lightPro[1])
      setTimeout(() => {
        offLight(document.querySelector('.active'))
      openLight(lightPro[2])
      setTimeout(()=>{
        offLight(document.querySelector('.active'))
        lit()
      },1000)
      }, 2000)
    }, 3000)
    }
    lit()*/
    

    /* 3.promise写法
    function clearTime(t){
      return new Promise(resolve=>{
        setTimeout(resolve,1000*t)
      })
    }
    function fn(){
      openLight(lightPro[0])
      clearTime(3).then(res=>{
        offLight(document.querySelector('.active'))
        openLight(lightPro[1])
        return clearTime(2)
      }).then(res=>{
        offLight(document.querySelector('.active'))
        openLight(lightPro[2])
        return clearTime(1)
      }).then(res=>{
        offLight(document.querySelector('.active'))
        fn()
      })
    }
    fn()*/

    // 4.属性链式操作
    function clearTime(t){
      return new Promise(resolve=>{
        setTimeout(resolve,1000*t)
      })
    }
    const p=new Proxy({},{
      get:function(target,propkey,receiver){

      }
    })
    
  </script>
</html>
